﻿using AutoMapper;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;
using Treinamento2015_01.Models;
using Treinamento2015_01Core.Infrastucture;
using Treinamento2015_01Core.Models;
using Treinamento2015_01Core.Service;

namespace Treinamento2015_01.Controllers
{
    public class StockController : BaseController
    {

        // GET: Stock
        public ActionResult Index()
        {
            List<StockViewModel> stockList = Mapper.Map<List<StockViewModel>>(new StockService().FindAll());
            Double TotalSum = 0;
            foreach (var item in stockList)
            {
                item.TotalValue = item.quantity * Convert.ToDouble(item.Product.Price);
                TotalSum += item.TotalValue;
                // TotalSum += item.TotalValue;
            }
            ViewBag.Products = new SelectList(new ProductService().FindAllActives(), "Id", "Name");

            ViewBag.TotalSum = TotalSum;
            return View(stockList);
        }

        // GET: Stock/Details/5
        public ActionResult Details(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            //if (stockViewModel == null)
            //{
            //    return HttpNotFound();
            //}
            return HttpNotFound();
        }

        // GET: Stock/Create
        public ActionResult Create()
        {
            return View();
        }

        // POST: Stock/Create
        // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
        // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create([Bind(Include = "Id,IdProduct,quantity")] StockViewModel stockViewModel)
        {
            if (ModelState.IsValid)
            {
                return RedirectToAction("Index");
            }

            return View(stockViewModel);
        }

        // GET: Stock/Edit/5
        public ActionResult Edit(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            //if (stockViewModel == null)
            //{
            //    return HttpNotFound();
            //}
            return HttpNotFound();
        }

        // POST: Stock/Edit/5
        // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
        // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit([Bind(Include = "Id,IdProduct,quantity")] StockViewModel stockViewModel)
        {
            if (ModelState.IsValid)
            {
                return RedirectToAction("Index");
            }
            return View(stockViewModel);
        }

        // GET: Stock/Delete/5
        public ActionResult Delete(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            //if (stockViewModel == null)
            //{
            //    return HttpNotFound();
            //}
            return HttpNotFound();
        }

        // POST: Stock/Delete/5
        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteConfirmed(int id)
        {
            return RedirectToAction("Index");
        }

        //ARRUMA ISSO
        [HttpPost, ActionName("ButtonClicked")]
        public ActionResult ButtonClicked([Bind(Include = "Id,IdProduct,quantity")] StockViewModel stockViewModel, String ButtonType)
        {
            if (ButtonType == "Add")
            {
                Stock stock = new StockService().FindByKey(stockViewModel.Id);

            }
            if (ButtonType == "Delete") { 
            
            }
            return new JsonResult() { 
                   Data = new { success = false, error = "Not a valid button was clicked!" }, 
                   JsonRequestBehavior = JsonRequestBehavior.AllowGet 
               };
        }
    }
}
